package cn.hx.admin.aop;

import cn.hx.admin.entity.Dept;

import cn.hx.admin.entity.User;

import cn.hx.admin.service.impl.DeptServiceImpl;
import cn.hx.admin.service.impl.OperLogServiceImpl;
import cn.hx.admin.util.RedisUtil;
import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;


@Component
@Aspect
public class OperLog {
    @Autowired
    RedisUtil<Object> redisUtil;
    @Autowired
    DeptServiceImpl deptService;
    @Autowired
    OperLogServiceImpl operLogService;

    /**
     * add
     * 此切面用于记录 添加 的操作记录
     *
     * @param joinPoint
     * @return
     */
     @Around("execution(* cn.hx.admin.controller.*.add(..))")
    public Object addLog(ProceedingJoinPoint joinPoint) throws Throwable {

        //业务类型为新增-->1
        // 获取 HttpServletRequest 对象
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        //调用接口
        Object target = joinPoint.getTarget();
        String title = target.getClass().getName();


        //方法名称
        String method = joinPoint.getSignature().getName();

        //请求方式
        String requestMethod = request.getMethod();
        //获取令牌
        String authorization = request.getHeader("Authorization");
        System.err.println("凭证：" + authorization);
        Object o = redisUtil.get(authorization);
        System.err.println(o);
        User user = (User) o;
        String username = user.getUsername();
        //部门ID
        Integer deptId = user.getDeptId();
        Dept dept = deptService.queryById(deptId);
        String deptName = dept.getName();

        //请求URL
        String operUrl = request.getRequestURL().toString();

        //主机地址
        String operId = request.getRemoteAddr();

        //请求参数
        Object[] args = joinPoint.getArgs();

        //操作时间
        Date date = new Date();
        //返回参数
        Object proceed = null;
        //操作状态
        Integer status = 0;
        try {
            proceed = joinPoint.proceed(args);
        } catch (Exception e) {
            status = 1;
        } finally {
            cn.hx.admin.entity.OperLog operLog = new cn.hx.admin.entity.OperLog();
            operLog.setTitle(title);//模块标题
            operLog.setBusinessType(1);//业务类型
            operLog.setMethod(method);//方法名称
            operLog.setRequestMethod(requestMethod);//请求方式
            operLog.setOperName(username);//操作人员
            operLog.setDeptName(deptName);//部门名称
            operLog.setOperUrl(operUrl);//请求URL
            operLog.setOperIp(operId);//主机地址
            operLog.setOperParam(Arrays.toString(args));//请求参数

            String stringProceed = String.valueOf(proceed);//转换返回参数为String
            operLog.setJsonResult(stringProceed);//返回参数
            operLog.setStatus(status);//操作状态
            operLog.setOperTime(new Date());//操作时间
            operLogService.insert(operLog);//添加进数据库

        }
        return proceed;
    }

    /**
     * deleteById
     * 此切面用于记录 删除 的操作记录
     *
     * @param joinPoint
     * @return
     */
    @Around("execution(* cn.hx.admin.controller.*.deleteById(..))")
    public Object deleteByIdLog(ProceedingJoinPoint joinPoint) throws Throwable {

        //业务类型为删除-->3
        // 获取 HttpServletRequest 对象
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //调用接口
        Object target = joinPoint.getTarget();
        String title = target.getClass().getName();

        //方法名称
        String method = joinPoint.getSignature().getName();


        //请求方式
        String requestMethod = request.getMethod();
        //获取令牌
        String authorization = request.getHeader("Authorization");
        Object o = redisUtil.get(authorization);
        User user = (User) o;
        String username = user.getUsername();
        //部门ID
        Integer deptId = user.getDeptId();
        Dept dept = deptService.queryById(deptId);
        String deptName = dept.getName();

        //请求URL
        String operUrl = request.getRequestURL().toString();

        //主机地址
        String operId = request.getRemoteAddr();

        //请求参数
        Object[] args = joinPoint.getArgs();

        //操作时间
        Date date = new Date();
        //返回参数
        Object proceed = null;
        //操作状态
        Integer status = 0;
        try {
            proceed = joinPoint.proceed(args);
        } catch (Exception e) {
            status = 1;
        } finally {
            cn.hx.admin.entity.OperLog operLog = new cn.hx.admin.entity.OperLog();
            operLog.setTitle(title);//模块标题
            operLog.setBusinessType(3);//业务类型
            operLog.setMethod(method);//方法名称
            operLog.setRequestMethod(requestMethod);//请求方式
            operLog.setOperName(username);//操作人员
            operLog.setDeptName(deptName);//部门名称
            operLog.setOperUrl(operUrl);//请求URL
            operLog.setOperIp(operId);//主机地址
            operLog.setOperParam(Arrays.toString(args));//请求参数

            String stringProceed = String.valueOf(proceed);//转换返回参数为String
            operLog.setJsonResult(stringProceed);//返回参数
            operLog.setStatus(status);//操作状态
            operLog.setOperTime(new Date());//操作时间
            operLogService.insert(operLog);//添加进数据库
            return proceed;
        }
    }

    /**
     * edit
     * 此切面用于记录 编辑 的操作记录
     *
     * @param joinPoint
     * @return
     */
    @Around("execution(* cn.hx.admin.controller.*.edit(..))")
    public Object editLog(ProceedingJoinPoint joinPoint) throws Throwable {

        //业务类型为编辑-->2
        // 获取 HttpServletRequest 对象
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //调用接口
        Object target = joinPoint.getTarget();
        String title = target.getClass().getName();

        //方法名称
        String method = joinPoint.getSignature().getName();

        //请求方式
        String requestMethod = request.getMethod();
        //获取令牌
        String authorization = request.getHeader("Authorization");
        Object o = redisUtil.get(authorization);
        User user = (User) o;
        String username = user.getUsername();
        //部门ID
        Integer deptId = user.getDeptId();
        Dept dept = deptService.queryById(deptId);
        String deptName = dept.getName();

        //请求URL
        String operUrl = request.getRequestURL().toString();

        //主机地址
        String operId = request.getRemoteAddr();

        //请求参数
        Object[] args = joinPoint.getArgs();

        //操作时间
        Date date = new Date();
        //返回参数
        Object proceed = null;
        //操作状态
        Integer status = 0;
        try {
            proceed = joinPoint.proceed(args);
        } catch (Exception e) {
            status = 1;
        } finally {
            cn.hx.admin.entity.OperLog operLog = new cn.hx.admin.entity.OperLog();
            operLog.setTitle(title);//模块标题
            operLog.setBusinessType(2);//业务类型
            operLog.setMethod(method);//方法名称
            operLog.setRequestMethod(requestMethod);//请求方式
            operLog.setOperName(username);//操作人员
            operLog.setDeptName(deptName);//部门名称
            operLog.setOperUrl(operUrl);//请求URL
            operLog.setOperIp(operId);//主机地址
            operLog.setOperParam(Arrays.toString(args));//请求参数

            String stringProceed = String.valueOf(proceed);//转换返回参数为String
            operLog.setJsonResult(stringProceed);//返回参数
            operLog.setStatus(status);//操作状态
            operLog.setOperTime(new Date());//操作时间
            operLogService.insert(operLog);//添加进数据库
            return proceed;
        }
    }

}

